package com.ogawa.project2213n.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ogawa.base.utils.HexUtil
import com.ogawa.project2213N.base.Base2213NMqttActivity
import com.ogawa.project2213n.R
import com.ogawa.project2213n.bean.Massage2213NArmchair
import com.ogawa.projectcommon.constants.UrlPathConstant
import kotlinx.android.synthetic.main.activity_advance.*


@Route(path = UrlPathConstant.ACTIVITY_2213N_ADVANCE)
class Device2213nAdvanceActivity : Base2213NMqttActivity(), View.OnClickListener {
 
    private var datas = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0)

    override fun getLayoutId(): Int {
        return R.layout.activity_advance
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.advance_massage))
        tv_jingjian.setOnClickListener(this)
        tv_jingjian.setOnClickListener(this)
        tv_tuibu.setOnClickListener(this)
        tv_yaobei.setOnClickListener(this)
        findViewById<TextView>(R.id.tv_cancel).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_confirm).setOnClickListener(this)
        setProgram()
    }

    private fun setProgram() {
        val deviceStatusBean = Massage2213NArmchair.getInstance().deviceStatusBean
        if (deviceStatusBean != null) {
            val advance = deviceStatusBean.functions.advanceAutoProgram.modelValue
            tv_jingjian.isSelected = advance.contains("1")
             tv_tuibu.isSelected = advance.contains("3")
            tv_tuibu.isSelected = advance.contains("4")
            tv_yaobei.isSelected = advance.contains("5")
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_jingjian,
             R.id.tv_tunbu,
            R.id.tv_tuibu,
            R.id.tv_yaobei -> v.isSelected = !v.isSelected
            R.id.tv_cancel -> {
                tv_jingjian.isSelected = false
                 tv_tuibu.isSelected = false
                tv_tuibu.isSelected = false
                tv_yaobei.isSelected = false
                finish()
            }
            R.id.tv_confirm -> {
                datas[7] = if (tv_jingjian.isSelected) 1 else 0
                 datas[5] = if (tv_tuibu.isSelected) 1 else 0
                datas[4] = if (tv_tuibu.isSelected) 1 else 0
                datas[3] = if (tv_yaobei.isSelected) 1 else 0
                val program = HexUtil.hexToInt(
                    HexUtil.bitToHex(datas))
                if (program != 0) {
                    setResult(Activity.RESULT_OK, Intent().putExtra("program", program.toString()))
                }
                finish()
            }
        }
    }

}